<?php
defined('_JEXEC') or die('Restricted access');

/**
 * @author binh.dn
 * @version 1.0
 * @created 28-Oct-2007 10:53:05 AM
 */
class HotelBusiness
{
	
    /**
     * @var JDatabase database object
     */
	var $db;
	
	function &getInstance(&$dbConn = null) {
	    static $instance;
	    if(!isset($intance)) {
	        $instance = &new HotelBusiness(&$dbConn);
	    }
	    return $instance;
	}
	
	/**
	 * constructor
	 * 
	 * @access private
	 */	
	function HotelBusiness(&$db = null)
	{
	    $this->db = $db;
        jimport('hotel.model.HHotel');
        jimport('hotel.model.HHotelDetail');
	}
	
	/**
	 * Insert new hotel to the database
	 * 
	 * @param array An associated array contains hotel parameters: UUID, name, description, lang, alias name
	 * @param JDatabase framework database object
	 */
	function addNewHotel($params) {
        $hotel = HHotel::fromArray($params);
        if(!$hotel->insert($this->db)) {
            echo "Error: " . $this->db->ErrorMsg();
            return false;
        }
        $params['Hotel_ID'] = $hotel->getID();
        $detail = HHotelDetail::fromArray($params);
        $detail->fromArray($params);
        if(!$detail->insert($this->db)) {
            echo "Error: ". $this->db->ErrorMsg();
            return false;
        }
        return true;
	}


	/**
	 * 
	 * @param hotel_id
	 */
	function getAllHotelSides($hotel_id)
	{
		
	}

	/**
	 * 
	 * @param hotel_id
	 */
	function &getHotel($hotel_id, $lang_id = 1){
	    $detail =& new HHotelDetail();
	    $detail->select($hotel_id, $lang_id, $this->db);
	    return $detail;
	}

	/**
	 * 
	 * @param sideView_id
	 */
	function getHotelSide($sideView_id)
	{
	}

	/**
	 * 
	 * @param array params an associated array contains hotel detail info
	 */
	function updateHotel($params) {
        if(!isset($params['Hotel_ID']) || !isset($params['Language_ID']) ) {
            echo "HotelBusiness: can not update hotel without key";
            return false;
        }
        jimport('hotel.model.HHotelDetail');
        $obj = HHotelDetail::fromArray($params);
        if(!$obj->update($this->db)) {
            echo "Error: " . $this->db->ErrorMsg() . '<br/>';
            return false;
        }
        return true;
	}

	/**
	 * list all hotels in the system
	 * 
	 * @return array array of HHotelDetails objects
	 */
	function listHotel() {
	    $ret =& HHotelDetail::selectSQL("SELECT * FROM HotelDetail ORDER BY Hotel_ID", $this->db);
	    if(!$ret) {
	        echo "Error: " . $this->db->ErrorMsg() . "<br/>";
	    }
	    return $ret;
	}
	
	/**
	 * Delete a hotel or a list of hotel
	 * 
	 * @params var hotel id or array of hotel id
	 */
	function removeHotel($cid) {
	    $conds = "ID in (" . implode(',', $cid) . ")";
	    jimport('hotel.model.HHotel');
	    if(!HHotel::deleteWithCond($conds, $this->db)) {
	        echo "Error: " . $this->db->ErrorMsg();
	        return false;
	    }
	    return true;
	}
}
?>